package code1_100.code1_10;

public class Code8 {

    static class Solution {
        public int myAtoi(String s) {
            if (s.trim().length()==0)
                return 0;
            String str = s.trim();
            int max = (int)(Math.pow(2,31)-1);
            int min = (int)Math.pow(-2,31);
            int sum=0;
            int tag=0;
            if(str.charAt(0)=='+')
                tag=1;
            else if (str.charAt(0)=='-')
                tag=-1;
            else if (str.charAt(0)<='9'&&str.charAt(0)>='0') {
                sum = str.charAt(0) - 48;
                tag=1;
            } else
                return sum;

            for (int i = 1; i <str.length() ; i++) {
                if (str.charAt(i)>'9'||str.charAt(i)<'0')
                    return sum*tag;
                int n = str.charAt(i)-48;
                if(sum*10/10!=sum) {
                    return tag == 1 ? max : min;
                }
                sum=sum*10+n;
                if (sum<0)
                    return tag == 1 ? max : min;

            }
            return sum*tag;
        }
    }

    public static void main(String[] args) {
        System.out.println(new Code8.Solution().myAtoi(
                "2147483648"));

    }


}
